<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" ref="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-button type="danger" size="small" @click="deleteBatch" icon="el-icon-delete">批量删除</el-button>
          <el-button type="warning" size="small" @click="openAddDialog" icon="el-icon-plus">添加</el-button>
        </el-form-item>
        <el-form-item label="客户姓名">
          <el-input v-model="formInline.name" placeholder="客户姓名"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="selectAll">查询</el-button>
          <el-button type="primary" plain size="small" @click="resetSearchForm('searchForm')" icon="el-icon-refresh">
            重置
          </el-button>
          <!-- <el-button type="primary" plain size="small" @click="dialogExcelVisible = true" icon="el-icon-upload2">导入
          </el-button>
          <el-button type="primary" plain size="small" @click="downloadExcel" icon="el-icon-download">导出</el-button> -->
        </el-form-item>
      </el-form>
    </el-card>
    <!--添加/更新对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="cus" :rules="cusRules" ref="cusRulesForm">
        <el-form-item label="客户姓名" prop="name">
          <el-input v-model="cus.name" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="联系手机号" prop="phone">
          <el-input v-model="cus.phone" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="公司单位" prop="company">
          <el-input v-model="cus.company" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input v-model="cus.address" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCus('cusRulesForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-card class="box-card">
      <el-table :data="cusList" @selection-change="handleSelectionChange" style="width: 100%">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户编码" align="center" prop="id" />
        <el-table-column label="客户姓名" align="center" prop="name" />
        <el-table-column label="联系手机号" align="center" prop="phone">
        </el-table-column>
        <el-table-column label="公司单位" align="center" prop="company" />
        <el-table-column label="创建时间" align="center" prop="createTime" />


        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit"
              @click="handleUpdate('编辑客户', scope.$index, scope.row)" >编辑</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="box-card">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getCusList,deleteById,deleteByIds,addCus,updateCus } from '@/api/cus'
export default {
  name: 'cus',
  data() {
    return {
      cusList: [],
      formInline: {
        name: ''
      },
      currentPage: 1,
      pageSize: 5,
      total: 0,
      ids: [],
      title: '添加客户',
      dialogFormVisible: false,
      cus: {
        name: '',
        phone: '',
        company: '',
        address: ''
      },
    }
  },
  methods: {
    list() {
      getCusList({ pageNum: this.currentPage, pageSize: this.pageSize, name: this.formInline.name }).then(res => {
        this.cusList = res.data.list
        this.total = res.data.total
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.list()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.list()
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除id为' + row.id + '的客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(row.id).then(() => {
          if (row.code == 200) {
            this.$message({
              type: 'info',
              message: '删除成功!'
            })
          }
        })
        this.list()
      }).catch(() => {
        this.list()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange(val) {
      console.log('line138',val)
      this.ids = [];
      for (let i = 0; i < val.length; i++) {
        this.ids.push(val[i].id);
      }
    },
    deleteBatch() {
      this.$confirm('此操作将永久删除选中的+' + this.ids.length + '个客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteByIds(this.ids).then(() => {
          this.$message({
            type: 'info',
            message: '删除成功!'
          })
          this.list()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    resetSearchForm(formName) {
      this.$refs[formName].resetFields()
      this.currentPage = 1
      this.formInline.name = ''
      this.list()
    },
    selectAll() {
      this.currentPage = 1
      this.list()
    },
    openAddDialog() {
      this.title = '添加客户'
      this.dialogFormVisible = true
      this.cus = {}
    },
    addCus(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title == "添加客户") {
            addCus(this.cus).then(res => {
              if (res.code == 200) {
                this.$message({
                  type: 'info',
                  message: '添加成功!'
                })
                this.dialogFormVisible = false
                this.list()
              }
            })
          }
          if (this.title == "编辑客户") {
            updateCus(this.cus).then(res => {
              if (res.code == 200) {
                this.$message({
                  type: 'info',
                  message: '编辑成功!'
                })
                this.dialogFormVisible = false
                this.list()
              }
            })
          }
        } else {
          return false
        }
      })
    },
    handleUpdate(title, index, row) {
      this.title = title
      this.dialogFormVisible = true
      this.cus = row
    },
  },
  created() {
    this.list()
  }
}  
</script>
<style lang="scss" scoped></style>